package com.qf.controller;

import com.qf.entiy.User;
import com.qf.service.Impl.UserServiceImpl;
import com.qf.service.UserService;

import com.qf.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.io.IOException;

/*
    Dell
    2021/10/9
    9:14
*/
@WebServlet("/user")
public class UserController extends BaseController {
    public String showByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("account");
        String password = request.getParameter("password");
        UserService userService  = new UserServiceImpl();
        User user = userService.login(username, password);
        System.out.println(user);
        request.getSession().setAttribute("user", user);
        return Constants.REDIRECT+"/index.jsp";
    }
    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("user");
        return Constants.REDIRECT+"/login.jsp";
    }
    public String checkPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        String oldPWD = request.getParameter("oldPwd");
        if(user.getPassword().equals(oldPWD)){
            return "1";
        }else{
            return "0";
        }
    }
    public String update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        String password = request.getParameter("repassword");
        UserService userService = new UserServiceImpl();
        System.out.println(user.getId());
        Integer update = userService.update(password, user.getId());
        return update.toString();
    }
}
